package com.spooky.sync.persistence.dao;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;
import com.spooky.sync.persistence.db.CreateSqlSyntax;
import com.spooky.sync.persistence.db.DataType;
import com.spooky.sync.persistence.db.ExtendedDAOBean;
import com.spooky.sync.persistence.entity.BaseEntity;
import com.spooky.sync.persistence.entity.Tag;

public class TagDAOBean extends ExtendedDAOBean<Tag> implements TagDAO, CreateSqlSyntax {
	private static final String TAG = "TagDAOBean";
	private static final String TABLE_NAME = "tag";
	private static final String COL_TAG = "t_tag";
	protected Map<String, DataType> mCollums = new LinkedHashMap<String, DataType>();

	public TagDAOBean(Context context) {
		super(context);
		initiate();
	}

	private TagDAOBean() {
		super();
		initiate();
	}

	private void initiate() {
		mCollums.put(COL_ID, DataType.ID);
		mCollums.put(COL_TAG, DataType.VARCHAR125);
	}

	@Override
	public Tag createSelect(Tag tag) {
		return null;
	}

	@Override
	public Tag create(Tag tag) {
		return super.create(tag);
	}

	@Override
	public Tag update(Tag tag) {
		return super.updateEntity(tag);
	}

	@Override
	public void delete(Tag tag) {
		super.deleteEntity(tag);
	}

	@Override
	public List<Tag> find(Tag tag) {
		return new LinkedList<Tag>();
	}

	@Override
	public Tag find(Long id) {
		return super.findEntity(id);
	}

	@Override
	public List<Tag> list(int start, int offset) {
		return super.listEntities();
	}

	@Override
	protected void assign(BaseEntity entity, Cursor result) throws Exception {
		Tag tag = (Tag) entity;
		tag.setName(result.getString(result.getColumnIndexOrThrow(COL_TAG)));
	}

	@Override
	protected int assign(BaseEntity entity, SQLiteStatement statement) throws SQLException {
		Tag tag = (Tag) entity;
		int count = 1;
		statement.bindString(count++, tag.getName());
		return count++;
	}

	@Override
	public String getTableName() {
		return TABLE_NAME;
	}

	@Override
	public String getCreateSyntax() {
		Iterator<Entry<String, DataType>> iterator = mCollums.entrySet().iterator();
		StringBuffer sql = new StringBuffer();
		sql.append("CREATE TABLE ");
		sql.append(TABLE_NAME);
		sql.append(" (\n");
		while (iterator.hasNext()) {
			Entry<String, DataType> next = iterator.next();
			sql.append(next.getKey());
			sql.append(" ");
			sql.append(next.getValue().getDataType());
			sql.append(",\n");
		}
		sql.delete(sql.length() - 2, sql.length());
		sql.append("\n);\n");
		return sql.toString();
	}

	@Override
	public String[] getCollumns() {
		String[] collumns = new String[mCollums.size()];
		Iterator<String> iterator = mCollums.keySet().iterator();
		int i = 0;
		while (iterator.hasNext()) {
			String next = iterator.next();
			collumns[i++] = next;
		}
		return collumns;
	}

	@Override
	protected Tag getEntity() {
		return new Tag();
	}

	@Override
	public String getChangeSyntax(int version) {
		return mSyntaxUpdate.get(version);
	}

	@Override
	public void close() {
		super.close();
	}
}
